package cn.lyn4ever.config;

import cn.lyn4ever.common.MyApiErrors;
import cn.lyn4ever.common.MyApiException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 微信公众号 “小鱼与Java”
 *
 * @date 2020/4/17
 * @auther Lyn4ever
 */
@ControllerAdvice
public class GlobalExceptionConfig {

    /**
     * 处理特定异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(MyApiException.class)
    public Object handler(MyApiException e) {
        return e.getApiErrors();
    }

    /**
     * 处理其他类型的运行期异常
     * @param e
     * @return
     */
    @ResponseBody
    @ExceptionHandler(RuntimeException.class)
    public Object handlerOther(RuntimeException e) {
        return MyApiErrors.FAILED;
    }

}
